package com.hiray.demos.accessibilityService

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.app.Notification
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import io.reactivex.Observable
import java.util.concurrent.TimeUnit

/**
 * Created by hiray on 2018/6/20.
 *@author hiray
 */
class AccessibilityServiceEx : AccessibilityService() {

    val typeMap = mapOf(
            0x00000001 to "TYPE_VIEW_CLICKED",
            /**[AccessibilityEvent.TYPE_VIEW_CLICKED]*/
            0x00000002 to "TYPE_VIEW_LONG_CLICKED",
            /**[AccessibilityEvent.TYPE_VIEW_LONG_CLICKED]*/
            0x00000008 to "TYPE_VIEW_FOCUSED",
            /**[AccessibilityEvent.TYPE_VIEW_FOCUSED]*/
            0x00000004 to "TYPE_VIEW_SELECTED",
            /**[AccessibilityEvent.TYPE_VIEW_SELECTED]*/
            0x00000010 to "TYPE_VIEW_TEXT_CHANGED",
            /**[AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED]*/
            0x00000020 to "TYPE_WINDOW_STATE_CHANGED",
            /**[AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED]*/
            0x00000040 to "TYPE_NOTIFICATION_STATE_CHANGED",
            /**[AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED]*/
            0x00000200 to "TYPE_TOUCH_EXPLORATION_GESTURE_START",
            /**[AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START]*/
            0x00000400 to "TYPE_TOUCH_EXPLORATION_GESTURE_END",
            /**[AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END]*/
            0x00000080 to "TYPE_VIEW_HOVER_ENTER",
            /**[AccessibilityEvent.TYPE_VIEW_HOVER_ENTER]*/
            0x00000100 to "TYPE_VIEW_HOVER_EXIT",
            /**[AccessibilityEvent.TYPE_VIEW_HOVER_EXIT]*/
            0x00001000 to "TYPE_VIEW_SCROLLED",
            /**[AccessibilityEvent.TYPE_VIEW_SCROLLED]*/
            0x00002000 to "TYPE_VIEW_TEXT_SELECTION_CHANGED",
            /**[AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED]*/
            0x00000800 to "TYPE_WINDOW_CONTENT_CHANGED",
            /**[AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED]*/
            0x00100000 to "TYPE_TOUCH_INTERACTION_START",
            /**[AccessibilityEvent.TYPE_TOUCH_INTERACTION_START]*/
            0x00200000 to "TYPE_TOUCH_INTERACTION_END",
            /**[AccessibilityEvent.TYPE_TOUCH_INTERACTION_END]*/
            0x00004000 to "TYPE_ANNOUNCEMENT",
            /**[AccessibilityEvent.TYPE_ANNOUNCEMENT]*/
            0x00040000 to "TYPE_GESTURE_DETECTION_START",
            /**[AccessibilityEvent.TYPE_GESTURE_DETECTION_START]*/
            0x00080000 to "TYPE_GESTURE_DETECTION_END",
            /**[AccessibilityEvent.TYPE_GESTURE_DETECTION_END]*/
            0x00008000 to "AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED",
            /**[AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED]*/
            0x00010000 to "AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED",
            /**[AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED]*/
            0x00020000 to "AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY",
            /**[AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY]*/
            0x00400000 to "AccessibilityEvent.TYPE_WINDOWS_CHANGED"
            /**[AccessibilityEvent.TYPE_WINDOWS_CHANGED]*/
    )

    val actionMap = mapOf(
            0x00000010 to "ACTION_CLICK",
            /**[AccessibilityNodeInfo.ACTION_CLICK]*/
            0x00000020 to "ACTION_LONG_CLICK",
            0x00000040 to "ACTION_ACCESSIBILITY_FOCUS",
            0x00000080 to "ACTION_CLEAR_ACCESSIBILITY_FOCUS",
            0x00000100 to "ACTION_NEXT_AT_MOVEMENT_GRANULARITY",
            0x00000200 to "ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY",
            0x00000400 to "ACTION_NEXT_HTML_ELEMENT",
            0x00000800 to "ACTION_PREVIOUS_HTML_ELEMENT",
            0x00001000 to "ACTION_SCROLL_FORWARD",
            0x00002000 to "ACTION_SCROLL_BACKWARD",
            0x00004000 to "ACTION_COPY",
            0x00008000 to "ACTION_PASTE",
            0x00010000 to "ACTION_CUT"
    )
    val TAG = "AccessibilityServiceEx"
    override fun onAccessibilityEvent(event: AccessibilityEvent) {

        val eventType = event.eventType
        val eventTime = event.eventTime
        val packageName = event.packageName
        val action = event.action
//        val sourceNodeInfo = event.source
        //和getSource方法得到的信息一样，但是需要在res/xml/accessibilityservice_config中配置"android:canRetrieveWindowContent="true""
        val sourceNodeInfo = rootInActiveWindow


        var parcelableData = event.parcelableData
        Log.i(TAG,
                "eventType: ${typeMap[eventType]} " +
                        "packageName: $packageName  " +
                        "action: ${actionMap[action]} " +
                        "NodeClassName: ${sourceNodeInfo.className} $parcelableData ")

        if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED ) {

            Log.i(TAG, "new Notification: text ----->")
            //下拉通知栏
//            performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS)
            performGlobalAction(AccessibilityService.GESTURE_SWIPE_UP)

//            Observable.just("")
//                    .delay(290, TimeUnit.MILLISECONDS)
//                    .subscribe {
//                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME)
////                        parcelableData.contentIntent.send()
//                    }
            //
//            backHomeAfterShortDelay()
        }
        //release
        sourceNodeInfo.recycle()
    }

    private fun backHomeAfterShortDelay() {
        Observable.just("")
                .delay(1020, TimeUnit.MILLISECONDS)
                .subscribe {

                }
    }

    override fun onInterrupt() {

    }

    /**
     * 代码配置，也可以在res/xml中建立文件（accessibilityservice_config）进行具体配置
     */
    override fun onServiceConnected() {
        super.onServiceConnected()
        val serviceInfo = AccessibilityServiceInfo()
        serviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK
        serviceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
        serviceInfo.notificationTimeout = 100
        //监听app自己发生的动态
        serviceInfo.packageNames = arrayOf("com.tencent.tim","com.eg.android.AlipayGphone","com.tencent.mobileqq")
        serviceInfo.flags = AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS or AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY
        setServiceInfo(serviceInfo)
    }
}